package com.base.cn.platform.os.common.mybatis;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 查询数据SQL提供类
 */
public class BaseSelectProvider extends BaseProvider{

    public String findByIdTableName(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        BigDecimal id = (BigDecimal)map.get("id");
        String tableName = (String)map.get("tableName");
        if(id==null){
            id = new BigDecimal(0);
        }
        String sql = getSQL(clazz,null,tableName,id,1,null);
        return sql;
    }
    /**
     * 通过ID查询数据
     * @param map
     * @return
     */
    public String findById(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        BigDecimal id = (BigDecimal)map.get("id");
        if(id==null){
            id = new BigDecimal(0);
        }
        String sql = getSQL(clazz,null,id,1,null);
        return sql;
    }

    public String findByIdContainTableName(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        BigDecimal id = (BigDecimal)map.get("id");
        String tableName = (String)map.get("tableName");
        List<String> containColumns = (List<String>)map.get("containColumns");
        String sql = getSQL(clazz,null,tableName,id,1,containColumns);
        return sql;
    }

    public String findByIdContain(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        BigDecimal id = (BigDecimal)map.get("id");
        List<String> containColumns = (List<String>)map.get("containColumns");
        String sql = getSQL(clazz,null,id,1,containColumns);
        return sql;
    }

    public String findWhereSqlTableName(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        String whereSql = (String) map.get("whereSql");//查询条件
        String tableName = (String) map.get("tableName");//查询条件
        Integer count = (Integer)map.get("count");//查询量
        List<String> containColumns = (List<String>)map.get("containColumns");//包含的列
        String sql = getSQL(clazz,whereSql,tableName,null,count,containColumns);
        return sql;

    }

    public String findWhereSql(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        String whereSql = (String) map.get("whereSql");//查询条件
        Integer count = (Integer)map.get("count");//查询量
        List<String> containColumns = (List<String>)map.get("containColumns");//包含的列
        String sql = getSQL(clazz,whereSql,null,count,containColumns);
        return sql;

    }

    /**
     * 分页查询
     * @param map
     * @return
     */
    public String findPage(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        String whereSql = (String) map.get("whereSql");//查询条件
        List<String> containColumns = (List<String>) map.get("containColumns");//如果有只要查询的列
        String sql = getSQL(clazz,whereSql,null,null,containColumns);
        return sql;
    }

    public String count(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        String whereSql = (String) map.get("whereSql");//查询条件
        String tableName = clazz.getSimpleName();
        String sql = "select count(*) from `"+tableName+"`";
        if(!StringUtils.isEmpty(whereSql)){
            sql +=" where "+whereSql;
        }
        return sql;
    }

    /**
     * sum查询
     * @param map
     * @return
     */
    public String sum(Map<String,Object> map){
        Class clazz = (Class)map.get("clazz");
        String whereSql = (String) map.get("whereSql");//查询条件
        String sumColumn = (String) map.get("sumColumn");//查询总量的列名
        String tableName = clazz.getSimpleName();
        String sql = "select sum(`"+sumColumn+"`) from `"+tableName+"`";
        if(!StringUtils.isEmpty(whereSql)){
            sql +=" where "+whereSql;
        }
        return sql;
    }

    private String getSQL(Class clazz,String whereSql,BigDecimal id,Integer count,List<String> containColumns){
        String tableName = clazz.getSimpleName();
        return this.getSQL(clazz,whereSql,tableName,id,count,containColumns);
    }

    private String getSQL(Class clazz,String whereSql,String tableName,BigDecimal id,Integer count,List<String> containColumns){
        SQL sql = new SQL();
        if(!ObjectUtils.isEmpty(containColumns)){
            for(String column : containColumns){
                sql.SELECT("`"+column+"`");
            }
        }else{
            sql.SELECT("*");
        }
        sql.FROM("`"+tableName+"`");
        if(id!=null){
            sql.WHERE(" id="+id);
        }
        String strSql = sql.toString();

        if(!StringUtils.isEmpty(whereSql)){
            strSql +=" where "+ whereSql;
        }
        if(count!=null && count.intValue()>0){
            strSql =strSql+" limit "+count;
        }
        return strSql;
    }

}
